package top.lishuoboy.javaweb.a05xml.xml2bean;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import top.lishuoboy.javaweb.a05xml.xml2bean.bean.School;
import top.lishuoboy.javaweb.a05xml.xml2bean.bean.Stu;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.StringWriter;
import java.util.Date;
import java.util.LinkedHashMap;

/**
 * JAXB(java11从jdk移除)
 */
public class JaxbTest {
    private static File file = FileUtil.touch(new File("tmp/05xml/Jaxb/school.xml"));

    public static void main(String[] args) throws JAXBException {
        bean2xml(); // 报文见 tmp/05xml/Jaxb/school.xml
        xml2bean();
    }

    /**
     * 一、bean转xml
     */
    private static void bean2xml() throws JAXBException {
        // 开始造数据
        School school = new School();
        // 1.JSON数值(XML不区分类型：数值、字符串、布尔)
        school.setA11Byte(Byte.MAX_VALUE);
        school.setA12Short(Short.MAX_VALUE);
        school.setA13Int(Integer.MAX_VALUE);
        school.setA14Long(Long.MAX_VALUE);
        school.setA15Float(Float.MAX_VALUE);
        school.setA16Double(Double.MAX_VALUE);
        // 2.JSON布尔(XML不区分类型：数值、字符串、布尔)
        school.setA17Boolean(true);
        // 3.JSON字符串(XML不区分类型：数值、字符串、布尔)
        school.setA18Char('c');
        // 3.1 java字符串转JSON字符串
        school.setB1String("str");
        school.setB2StringBuffer(new StringBuffer("StringBuffer"));
        school.setB3StringBuilder(new StringBuilder("StringBuilder"));
        // 3.2 java日期转JSON字符串
        school.setC1Date(new Date());
//        school.setC2Instant(Instant.now());
//        school.setC3LocalDateTime(LocalDateTime.now());
        // 4.JSON对象--------------------------------------
        Stu stu1 = new Stu();
        stu1.setName("stu1");

        Stu stu2 = new Stu();
        stu2.setName("stu2");

        LinkedHashMap map = new LinkedHashMap();
        map.put("ValIsNull", null);
        map.put("ValIsInt", 1);
        map.put("ValIsBoolean", false);
        map.put("ValIsChar", 'c');
        map.put("ValIsString", "str");
        map.put("ValIsStringBuffer", new StringBuffer("StringBuffer"));
        map.put("ValIsStringBuilder", new StringBuilder("StringBuilder"));
        map.put("ValIsDate", new Date());
//        map.put("ValIsInstant", Instant.now());
//        map.put("ValIsLocalDateTime", LocalDateTime.now());

        map.put(null, "KeyIsNull");
        map.put(1, "KeyIsInt");
        map.put(false, "KeyIsBoolean");
        map.put('c', "KeyIsChar");
        map.put("str", "KeyIsString");
        map.put(new StringBuffer("StringBuffer"), "KeyIsStringBuffer");
        map.put(new StringBuilder("StringBuilder"), "KeyIsStringBuilder");
        map.put(new Date(), "KeyIsDate");
//        map.put(Instant.now(), "KeyIsInstant");
//        map.put(LocalDateTime.now(), "KeyIsLocalDateTime");

        // 4.1 自定义对象Stu
        school.setD1Stu(stu1);
        // 4.2 Map
        school.setD2Map(map);

        // 5 JSON数组--------------------------------------
        // 5.1  INT数组
        school.setE1IntArr(new int[]{1, 2, 3});
        // 5.2 String数组
        school.setE2StrArr(new String[]{"str1", "str2", "str3"});
        // 5.3 自定义对象Stu数组
        school.setE3StuArr(new Stu[]{stu1, stu1, stu2});
        // 5.4 List
        school.setE4List(ListUtil.list(false, null, Integer.MAX_VALUE, true, 'c', "str", new StringBuilder("StringBuilder")));

        // 转xml
        JAXBContext jaxbContext = JAXBContext.newInstance(School.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        //格式化输出，即按标签自动换行，否则就是一行输出
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        //设置编码（默认编码就是utf-8）
        marshaller.setProperty(Marshaller.JAXB_ENCODING, CharsetUtil.UTF_8);
        //是否省略xml头信息，默认不省略（false）
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
        StringWriter writer = new StringWriter();
        marshaller.marshal(school, writer);
        // 输出xml字符串
//        System.out.println(writer);
        System.out.println("xml字符串文件：" + file);
        //输出到文件
        marshaller.marshal(school, file);
    }

    /**
     * 二、xml字符串转bean
     */
    private static void xml2bean() throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(School.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        School school = (School) unmarshaller.unmarshal(file);
        System.out.println(school);
    }
}
